home *** CD-ROM | disk | FTP | other *** search
- unit COMDragDropU;
-
- {$ifdef Ver100} { Delphi 3.0x }
- {$define DelphiLessThan4}
- {$endif}
- {$ifdef Ver110} { C++ Builder 3.0x }
- {$define DelphiLessThan4}
- {$endif}
-
- {$define ListFormats}
-
- interface
-
- uses
- ActiveX, //for IDropTaget
- COMDragDropSupport, //for TDataFormats
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ExtCtrls, OleCtnrs, StdCtrls, ComCtrls, ToolWin;
-
- type
- TForm1 = class(TForm, IUnknown, IDropTarget)
- ListBox1: TListBox;
- pcDragDisplay: TPageControl;
- tsText: TTabSheet;
- memText: TMemo;
- tsRTF: TTabSheet;
- reRTF: TRichEdit;
- tsBitmap: TTabSheet;
- imgBitmap: TImage;
- tsDIB: TTabSheet;
- tsWMF: TTabSheet;
- tsEMF: TTabSheet;
- imgDIB: TImage;
- imgWMF: TImage;
- tsHDrop: TTabSheet;
- lstHDrop: TListBox;
- imgEMF: TImage;
- Splitter1: TSplitter;
- tsFileName: TTabSheet;
- lblFileName: TLabel;
- tsShellIDList: TTabSheet;
- lstShellIDList: TListBox;
- tsShellIDListOffset: TTabSheet;
- lstShellIDListOffset: TListBox;
- tsObjDesc: TTabSheet;
- lstObjDesc: TListBox;
- tsLinkSrcDesc: TTabSheet;
- lstLinkSrcDesc: TListBox;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- DataObject: TDataObject;
- {$ifdef DelphiLessThan4}
- //IUnknown
- function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
- function _AddRef: Integer; stdcall;
- function _Release: Integer; stdcall;
- {$endif}
- //IDropTarget
- function DragEnter(const dataObj: IDataObject; grfKeyState: Longint;
- pt: TPoint; var dwEffect: Longint): HResult; stdcall;
- function DragOver(grfKeyState: Longint; pt: TPoint;
- var dwEffect: Longint): HResult;
- {$ifndef DelphiLessThan4}reintroduce; {$endif}stdcall;
- function DragLeave: HResult; stdcall;
- function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint;
- var dwEffect: Longint): HResult; stdcall;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- uses
- ComObj, //for OleCheck
- RichEdit, //for EM_GETOLEINTERFACE message
- RichOle; //for IRichEditOle & TREObject
-
- {$R *.DFM}
-
- {$ifdef DelphiLessThan4}
- //IUnknown
- function TForm1._AddRef: Integer;
- begin
- if VCLComObject = nil then
- Result := -1 // -1 indicates no reference counting is taking place
- else
- Result := IVCLComObject(VCLComObject)._AddRef;
- end;
-
- function TForm1._Release: Integer;
- begin
- if VCLComObject = nil then
- Result := -1 // -1 indicates no reference counting is taking place
- else
- Result := IVCLComObject(VCLComObject)._AddRef;
- end;
-
- function TForm1.QueryInterface(const IID: TGUID;
- out Obj): HResult;
- begin
- if VCLComObject = nil then
- begin
- if GetInterface(IID, Obj) then Result := S_OK
- else Result := E_NOINTERFACE
- end
- else
- Result := IVCLComObject(VCLComObject).QueryInterface(IID, Obj);
- end;
- {$endif}
-
- //IDropTarget
- function TForm1.DragEnter(const dataObj: IDataObject; grfKeyState: Integer;
- pt: TPoint; var dwEffect: Integer): HResult;
- begin
- DataObject := TDataObject.Create(dataObj);
- {$ifdef ListFormats}
- DataObject.ListFormats(ListBox1.Items);
- {$endif}
- //Check a supported item is being dragged
- if [dfText, dfHDrop, dfDIB, dfBitmap,
- dfWMF, dfEMF, dfRTF, dfFileName,
- dfShellIDList] *
- DataObject.DataFormats <> [] then
- dwEffect := DROPEFFECT_COPY
- else
- dwEffect := DROPEFFECT_NONE;
- Result := S_OK
- end;
-
- function TForm1.DragLeave: HResult;
- begin
- Result := S_OK;
- //Tidy up
- DataObject.Free;
- DataObject := nil
- end;
-
- function TForm1.DragOver(grfKeyState: Integer; pt: TPoint;
- var dwEffect: Integer): HResult;
- begin
- //dwEffect :=
- Result := S_OK;
- end;
-
- function TForm1.Drop(const dataObj: IDataObject; grfKeyState: Integer;
- pt: TPoint; var dwEffect: Integer): HResult;
- var
- Txt: String;
- RichEditOle: IRichEditOle;
- Loop: Integer;
- begin
- for Loop := 0 to pcDragDisplay.PageCount - 1 do
- pcDragDisplay.Pages[Loop].TabVisible := False;
-
- if dfDIB in DataObject.DataFormats then
- begin
- tsDIB.TabVisible := True;
- DataObject.GetDataAsDIB(imgDIB.Picture.Bitmap);
- end;
-
- if dfBitmap in DataObject.DataFormats then
- begin
- tsBitmap.TabVisible := True;
- DataObject.GetDataAsBitmap(imgBitmap.Picture.Bitmap);
- end;
-
- if dfEMF in DataObject.DataFormats then
- begin
- tsEMF.TabVisible := True;
- DataObject.GetDataAsEMF(imgEMF.Picture.Metafile);
- end;
-
- if dfWMF in DataObject.DataFormats then
- begin
- tsWMF.TabVisible := True;
- DataObject.GetDataAsWMF(imgWMF.Picture.Metafile);
- end;
-
- if dfRTF in DataObject.DataFormats then
- begin
- tsRTF.TabVisible := True;
- reRTF.Lines.Clear;
- //Try and get richedit to deal with it...
- if reRTF.Perform(EM_GETOLEINTERFACE, 0, LParam(@RichEditOle)) <> 0 then
- RichEditOle.ImportDataObject(dataObj, 0, 0)
- else
- begin
- //If it can't, do it yourself
- DataObject.GetDataAsRTF(Txt);
- reRTF.Lines.Text := Txt
- end;
- end;
-
- if dfText in DataObject.DataFormats then
- begin
- tsText.TabVisible := True;
- DataObject.GetDataAsText(Txt);
- memText.Text := Txt;
- end;
-
- if dfHDrop in DataObject.DataFormats then
- begin
- tsHDrop.TabVisible := True;
- DataObject.GetDataAsHDrop(lstHDrop.Items);
- end;
-
- if dfFileName in DataObject.DataFormats then
- begin
- tsFileName.TabVisible := True;
- DataObject.GetDataAsFileName(Txt);
- lblFileName.Caption := Txt;
- end;
-
- if dfShellIDList in DataObject.DataFormats then
- begin
- tsShellIDList.TabVisible := True;
- DataObject.GetDataAsShellIDList(lstShellIDList.Items);
- end;
-
- if dfObjectDescriptor in DataObject.DataFormats then
- begin
- tsObjDesc.TabVisible := True;
- DataObject.GetDataAsObjectDescriptor(lstObjDesc.Items);
- end;
-
- if dfLinkSrcDescriptor in DataObject.DataFormats then
- begin
- tsLinkSrcDesc.TabVisible := True;
- DataObject.GetDataAsLinkSrcDescriptor(lstLinkSrcDesc.Items);
- end;
-
- //Tidy data object up
- DragLeave;
-
- Result := S_OK;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- var
- Loop: Integer;
- begin
- OleCheck(RegisterDragDrop(Handle, Self));
- {$ifndef ListFormats}
- ListBox1.Hide;
- {$endif}
- for Loop := 0 to pcDragDisplay.PageCount - 1 do
- pcDragDisplay.Pages[Loop].TabVisible := False;
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- OleCheck(RevokeDragDrop(Handle))
- end;
-
- initialization
- OleCheck(OleInitialize(nil))
- finalization
- OleUninitialize
- end.
-